package com.joey.gateway.handler;



import com.joey.gateway.Exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice  //统一异常处理
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result exceptionHandler(BaseException ex){
        log.error("异常信息：{}", ex.getMessage());
        return Result.error(ex.getMessage());
    }

    /*
    *   重复姓名异常抛出
    *  */
//    @ExceptionHandler
//    public Result exceptionHandler(SQLIntegrityConstraintViolationException ex){
//    //eg: SQLIntegrityConstraintViolationException: Duplicate entry 'lisi' for key 'employee.idx_username'
//        //获取异常信息
//        String exMessage = ex.getMessage();
//        if(exMessage.contains("Duplicate entry")){
//            String[] split = exMessage.split(" ");
//            String userName = split[2];
//            String msg = userName + "MessageConstant.ACCOUNT_ALREADY_EXIST";
//            return Result.error(msg);
//        }else{
//            return Result.error("未知错误");
//        }
//
//    }

}
